<?php
/*
	Muffinz CMS - A website content management system.
	Copyright (C) 2010-2011 Sigitas Zelenkovas

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/

class Register{
	private $data = array();
	private $_e = "";
	
	public function Data($input == false){
		if(!$input){
			return false;
		}else {
			$this->data = $input;
		}
	}
	
	public function VerifyData($input = false){
		if(!$input){
			return false;
		}else{
			if(!$this->ValidateName($this->data['username'])){ 		return false; }
			if(!$this->ValidatePassword($this->data['password'])){ 	return false; }
		}
	}
	
	public function ValidatePassword($p1 = false, $p2 = false){
		if(!$p1 && !$p2){ return false; }
		if($p1 != $p2){
			$this->_e = "Password and its confirmtion does not match"; 
			return false;
		}
		if(!((strlen($p1) >= $_SESSION['sys']['user_pass_minlen'] && strlen($p1) <= $_SESSION['sys']['user_pass_maxlen']) ||
			 (strlen($p2) >= $_SESSION['sys']['user_pass_minlen'] && strlen($p2) <= $_SESSION['sys']['user_pass_maxlen']))){
			$this->_e = "Password lenght does not fit into acceptable bounds. (minimum lenght: {$_SESSION['sys']['user_pass_minlen']}, maximum:{$_SESSION['sys']['user_pass_maxlen']})";
			return false;
		}
	}
	
	public function ValidateName($username = false);
		if(!$username){ return false; }
		if(!(strlen($username) >= $_SESSION['sys']['user_name_minlen'] && strlen($username) <= $_SESSION['sys']['user_name_maxlen'])){
			$this->_e = "User name length is not in bounds (minimum length is: {$_SESSION['sys']['user_name_minlen']}, maximum: {$_SESSION['sys']['user_name_maxlen']})";
			return false;
		}
		if(!preg_match("/^[a-zA-Z ]+$/", $_POST['rname'])){
			return false;
		}
		
		return true;
	}
}